package com.example.fuluchou.service.activitystatus.operator;

import com.example.fuluchou.dao.dataobject.ActivityPrizeDO;
import com.example.fuluchou.dao.mapper.ActivityPrizeMapper;
import com.example.fuluchou.service.dto.ConvertActivityStatusDTO;
import com.example.fuluchou.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 奖品模块
 */
@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Override
    public Integer sequence() {
        return 1;
    }

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActicityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if(null == activityId
                || null == targetPrizeStatus) {
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(null == activityId){
            return false;
        }
        //判断 当前奖品状态和目标奖品状态是否一致
        if(targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActicityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId,prizeId,targetPrizeStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }

    }
}
